<?php

namespace app\admin\builder;
class Form extends Base
{

    private $_data = [];
    private $_type = [];

    public function setTitle($title)
    {
        $this->_data['seo_title'] = $title;
        return $this;
    }

    public function saveConfig($return = false)
    {
        $time = time();
        $data = [];
        foreach (input() as $key => $val) {
            if ($key == 'file') {
                continue;
            }
            if (is_array($val)) {
                $val = implode(',', array_keys($val));
            }

            if (strtotime($val) !== false) {
                $val = strtotime($val);
            };


            $data[] = ['key' => $key, 'value' => $val, 'update_time' => $time];
        }
        $rs = db('config')->insertAll($data, true);

        if (!$return) {
            if ($rs) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        return $rs;
    }

    public function typeText($name, $title, $ext = [])
    {

        return $this->setKey('text', $name, $title, $ext);
    }

    public function typePassword($name, $title, $ext = [])
    {

        return $this->setKey('password', $name, $title, $ext);
    }

    public function typeSelect($name, $title, $ext = [])
    {
        return $this->setKey('select', $name, $title, $ext);
    }

    public function typeSwitch($name, $title, $ext = [])
    {

        return $this->setKey('switch', $name, $title, $ext);
    }

    public function typeCheckbox($name, $title, $ext = [])
    {
        return $this->setKey('checkbox', $name, $title, $ext);
    }

    public function typeRadio($name, $title, $ext = [])
    {
        return $this->setKey('radio', $name, $title, $ext);
    }

    public function typeTextarea($name, $title, $ext = [])
    {

        return $this->setKey('textarea', $name, $title, $ext);
    }

    public function typeUploadImage($name, $title, $ext = [])
    {

        return $this->setKey('upload_image', $name, $title, $ext);
    }

    public function typeArea($name, $title, $ext = [])
    {

        return $this->setKey('area', $name, $title, $ext);
    }


    public function typeTime($name, $title, $ext = [])
    {

        return $this->setKey('time', $name, $title, $ext);
    }

    public function setData($data = [])
    {
        $this->_data['data'] = $data;
        return $this;
    }

    public function typeEditor($name, $title, $ext = [])
    {
        $this->_data['lay_js'][] = "var editor_{$name} = layedit.build('LAY_{$name}_editor');edit_array.push(editor_{$name})";
        return $this->setKey('editor', $name, $title, $ext);
    }

    public function form($action, $method = 'post', $ext = [])
    {

        $this->_data['form'] = array_merge(['action' => $action, 'method' => $method], $ext);
        return $this;
    }


    public function setKey($type, $name, $title, $ext = [])
    {
        $ext = $ext ?: [];

        if (!isset($ext['option']) && in_array($type, ['radio', 'checkbox', 'select'])) {
            $t = $ext;
            $ext = [];
            $ext['option'] = $t;
        }
        if (isset($ext['option'])) {
            if (!is_array(reset($ext['option']))) {
                $opt = [];
                foreach ($ext['option'] as $k => $v) {
                    $opt[] = ['value' => $k, 'title' => $v];
                }
                $ext['option'] = $opt;
                unset($opt);
                unset($v);
            }
        }
        $this->_data['list'][] = array_merge(['type' => $type, 'name' => $name, 'title' => $title], $ext);
        $this->_type[$name] = $type;
        return $this;
    }

    public function show()
    {

        if (empty($this->_data['data'])) {
            $this->_data['data'] = [];
            $keys = array_keys($this->_type);
            $data = db('config')->where('key','in', $keys)->field('key,value')->select();
            foreach ($data as $v) {
                switch ($this->_type[$v['key']]) {
                    default:
                }
                $this->_data['data'][$v['key']] = $v['value'];
            }
        }

        !empty($this->_data['lay_js']) && $this->_data['lay_js'] = implode("\n", $this->_data['lay_js']);
        $this->assign($this->_data);
        return view(dirname(dirname(__FILE__)) . '/builder/view/form.html');
    }


}
